package com.yzt.sys.service.impl;

import com.yzt.sys.entity.Menu;
import com.yzt.sys.mapper.MenuMapper;
import com.yzt.sys.service.IMenuService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yzt.base.dto.MenuDto;
import com.yzt.base.dto.ZTreeDTO;
import com.yzt.base.service.impl.BaseServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统菜单表 服务实现类
 * </p>
 *
 * @author Y
 * @since 2020-12-16
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, Menu> implements IMenuService {

	@Override
	public List<Menu> getMenuList(Long parentId) {
		List<Menu> menuList = this
				.list(Wrappers.lambdaQuery(Menu.class).eq((parentId != null), Menu::getParentmenuId, parentId)
						.isNull((parentId == null), Menu::getParentmenuId));
		return menuList;
	}

	public List<Menu> getMenuList(Long parentId, List<Long> menuIds) {
		List<Menu> menuList = this
				.list(Wrappers.lambdaQuery(Menu.class).eq((parentId != null), Menu::getParentmenuId, parentId)
						.isNull((parentId == null), Menu::getParentmenuId)
						.in(menuIds != null && menuIds.size() > 0, Menu::getId, menuIds));
		return menuList;
	}

	@Override
	public List<MenuDto> getMenuDtoList() {
		List<MenuDto> list = new ArrayList<MenuDto>();
		List<Menu> menuList = this.getMenuList(null);
		for (Menu menu : menuList) {
			MenuDto dto = new MenuDto();
			BeanUtils.copyProperties(menu, dto);
			list.add(dto);
			getMenuChildList(dto);
		}
		return list;
	}

	private void getMenuChildList(MenuDto menuDto) {
		List<MenuDto> list = new ArrayList<MenuDto>();
		List<Menu> menuList = this.getMenuList(menuDto.getId());
		for (Menu menu : menuList) {
			MenuDto dto = new MenuDto();
			BeanUtils.copyProperties(menu, dto);
			list.add(dto);
			getMenuChildList(dto);
		}
		menuDto.setChild(list);
	}

	@Override
	public List<MenuDto> getMenuDtoList(List<Long> menuIds) {
		List<MenuDto> list = new ArrayList<MenuDto>();
		List<Menu> menuList = this.getMenuList(null, menuIds);
		for (Menu menu : menuList) {
			MenuDto dto = new MenuDto();
			BeanUtils.copyProperties(menu, dto);
			list.add(dto);
			getMenuChildList(dto, menuIds);
		}
		return list;
	}

	private void getMenuChildList(MenuDto menuDto, List<Long> menuIds) {
		List<MenuDto> list = new ArrayList<MenuDto>();
		List<Menu> menuList = this.getMenuList(menuDto.getId(), menuIds);
		for (Menu menu : menuList) {
			MenuDto dto = new MenuDto();
			BeanUtils.copyProperties(menu, dto);
			list.add(dto);
			getMenuChildList(dto, menuIds);
		}
		menuDto.setChild(list);
	}

	@Override
	public List<ZTreeDTO> getMenuZTreeDto() {
		List<ZTreeDTO> list = new ArrayList<>();
		List<Menu> menuList = this.getMenuList(null);
		for (Menu menu : menuList) {
			ZTreeDTO dto = new ZTreeDTO();
			dto.setId(menu.getId());
			dto.setName(menu.getMenuText());
			dto.setUrl(menu.getMenuUrl());
			dto.setOpen(true);
			list.add(dto);
			getZTreeChildList(dto);
		}
		return list;
	}

	private void getZTreeChildList(ZTreeDTO zTreeDTO) {
		List<ZTreeDTO> list = new ArrayList<>();
		List<Menu> menuList = this.getMenuList(zTreeDTO.getId());
		for (Menu menu : menuList) {
			ZTreeDTO dto = new ZTreeDTO();
			dto.setId(menu.getId());
			dto.setName(menu.getMenuText());
			dto.setUrl(menu.getMenuUrl());
			dto.setOpen(true);
			list.add(dto);
			getZTreeChildList(dto);
		}
		zTreeDTO.setChildren(list);
	}

	@Override
	public List<Menu> getMenuListByIds(List<Long> ids) {
		List<Menu> list = this.listByIds(ids);
		return list;
	}

}
